const socket = io('/teacher');

// 初始化界面元素
const questionContainer = document.getElementById('question-container');
const statsContainer = document.getElementById('stats-container');
const publishBtn = document.getElementById('publish-btn');
const nextBtn = document.getElementById('next-btn');

// 监听服务器推送的题目更新
socket.on('currentQuestion', ({ question, isPublished }) => {
    questionContainer.innerHTML = `
        <h3>${question.question}</h3>
        <p>选项：${question.options.join(', ')}</p>
        <p>状态：${isPublished ? '已发布' : '未发布'}</p>
    `;
    publishBtn.disabled = isPublished;
});

// 监听答案统计更新
socket.on('answerStats', stats => {
    statsContainer.innerHTML = Object.entries(stats)
        .map(([option, count]) => `${option}: ${count}人`)
        .join('<br>');
});

// 按钮点击事件
publishBtn.addEventListener('click', () => socket.emit('publishQuestion'));
nextBtn.addEventListener('click', () => socket.emit('nextQuestion'));